import '../index.dart';
import 'package:intl/intl.dart';

class TodoItem extends StatefulWidget {
  TodoItem(this.type, this.todo) : super(key: ValueKey(todo.taskId));

  late int type;
  final Todo todo;

  @override
  _TodoItemState createState() => _TodoItemState();
}

class _TodoItemState extends State<TodoItem> {
  @override
  Widget build(BuildContext context) {
    // DateTime now = DateTime.now();
    //
    // DateTime planTime =
    //     DateFormat('yyyy-MM-dd HH:mm:ss').parse(widget.todo.planTime ?? "");
    // DateTime.fromMillisecondsSinceEpoch((widget.todo.planTime ?? 0) as int);

    // Duration duration = now.difference(planTime);
    // String remainingTime = (duration.isNegative ? "剩余" : "超时") +
    //     (duration.inDays != 0 ? "${duration.inDays.abs()}天" : "") +
    //     (duration.inHours != 0 ? "${duration.inHours.abs() % 3600}时" : "") +
    //     "${duration.inSeconds.abs() % 60}分";
    return InkWell(
      child: Padding(
        padding: const EdgeInsets.only(left: 16, top: 16, bottom: 16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Row(
              // crossAxisAlignment: CrossAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Container(
                  child: Image.asset((widget.todo.flowType == "任务")
                      ? "images/home_type_task.png"
                      : "images/home_type_approval.png"),
                  padding: EdgeInsets.only(top: 5),
                ),
                const SizedBox(
                  height: 40,
                  width: 8,
                ),
                Expanded(
                  child: Text(
                    widget.todo.taskTitle ?? "",
                    // overflow: TextOverflow.ellipsis,
                    // maxLines: 1,
                    style: Theme.of(context).textTheme.titleMedium,
                  ),
                ),
                const SizedBox(
                  height: 40,
                  width: 8,
                ),
                widget.type == 0 && (widget.todo.remainingTime ?? "").isNotEmpty
                    ? Material(
                        color:
                            (widget.todo.remainingTime ?? "").startsWith("剩余")
                                ? Theme.of(context).colorScheme.primary
                                : Theme.of(context).colorScheme.error,
                        // elevation: 1.0,
                        // 阴影颜色
                        borderRadius: const BorderRadius.only(
                            bottomLeft: Radius.circular(12)),
                        // type: MaterialType.button,
                        child: Container(
                          padding: const EdgeInsets.only(
                              top: 5, left: 10, right: 10, bottom: 5),
                          child: Text(
                            widget.todo.remainingTime ?? "",
                            style: Theme.of(context)
                                .textTheme
                                .labelMedium
                                ?.copyWith(color: Colors.white),
                          ),
                        ),
                      )
                    : Container()
              ],
            ),
            Padding(
              padding: const EdgeInsets.only(right: 16),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  const SizedBox(
                    height: 10,
                  ),
                  Row(
                    children: [
                      Text(
                        widget.type == 1 ? '处理时间：' : '发起时间：',
                        style: Theme.of(context).textTheme.bodyMedium?.copyWith(
                            color: Theme.of(context).colorScheme.onSecondary),
                      ),
                      Text(
                        widget.type == 1
                            ? widget.todo.taskCompleteTime ?? ""
                            : widget.todo.taskCreateTime ?? "",
                        style: Theme.of(context).textTheme.bodyMedium?.copyWith(
                            color: Theme.of(context).colorScheme.onPrimary),
                      ),
                    ],
                  ),
                  const SizedBox(
                    height: 10,
                  ),
                  Row(
                    children: [
                      Text(
                        '发起人：',
                        style: Theme.of(context).textTheme.bodyMedium?.copyWith(
                            color: Theme.of(context).colorScheme.onSecondary),
                      ),
                      Text(
                        widget.todo.submitUserName ?? "",
                        style: Theme.of(context).textTheme.bodyMedium?.copyWith(
                            color: Theme.of(context).colorScheme.onPrimary),
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
      onTap: () {
        if (widget.todo.processDefinitionKey == "product_order") {
          Navigator.of(context).push(MaterialPageRoute(
              settings: const RouteSettings(name: "WorkOrderDetails"),
              builder: (context) => WorkOrderDetailsPage(
                    orderId: widget.todo.businessKey!,
                    taskId: widget.todo.taskId,
                  )));
        } else {
          showToast("暂不支持");
        }
      },
    );
  }
}
